package org.devops

def getChangeLog() {
    def changeString = ""
    def MAX_MSG_LEN = 20
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++) {
        def entries = changeLogSets[i].items
        for (int j = 0; j < entries.length; j++) {
            def entry = entries[j]
            truncatedMsg = entry.msg.take(MAX_MSG_LEN)
            commitTime = new Date(entry.timestamp).format("MM-dd HH:mm")
            changeString += " - ${truncatedMsg} [${entry.author} ${commitTime}]\n"
        }
    }
    if (!changeString) {
        changeString = " - No changes"
    }
    return changeString
}

def send(robotId, appName, tagName, status, catchInfo) {
    def changeLog = getChangeLog()

    dingtalk (
            robot: "${robotId}",
            type: "MARKDOWN",
            title: "项目构建信息",
            text: [">  构建信息\n- 应用名称: ${appName}\n- 构建结果: ${status} ${catchInfo}\n- 发起人员: ${env.triggerUser}\n- 构建版本: ${tagName}\n- 持续时间: ${currentBuild.durationString.minus(' and counting')}\n- 构建地址:\n${env.BUILD_URL}console\n- 更新日志:\n${changeLog}"]
    )
}
